package com.wyg.blog.advice;

import com.qiniu.util.Json;
import com.wyg.blog.utils.APIException;
import com.wyg.blog.utils.JsonResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.mail.MessagingException;
import org.springframework.mail.MailSendException;

/**
 * @description:
 * @author: WYG
 * @time: 2021/2/3 0:20
 */
@RestControllerAdvice
public class ExceptionControllerAdvice {

    //建议项目上线时这么做,能够屏蔽掉错误信息暴露给前端,在实际开发中方便调试使用上述方法,可以清楚查看错误原因
    @ExceptionHandler(APIException.class)
    public JsonResult<String> APIExceptionHandler(APIException e) {
        return new JsonResult<>("api failed", "1001", e.getMessage());
    }

    @ExceptionHandler(MessagingException.class)
    public JsonResult<String> Messaging(MessagingException e) {
        return new JsonResult<>("api filed", "1002", "未知原因!");
    }

    @ExceptionHandler(MailSendException.class)
    public JsonResult<String> MailSend(MailSendException e) {
        return new JsonResult<>("sending mail filed", "200", "留言成功!");
    }

}
